package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.jpa.TblCategoria;
import com.mastercard.assets.jpa.TblFlujo;
import com.mastercard.assets.jpa.TblPreimplementacion;
import com.mastercard.assets.jpa.TblSubcateg;
import com.mastercard.assets.jpa.TblTarea;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AssetAltaForm;
import com.mastercard.assets.web.struts.forms.AssetBorrarForm;
import com.mastercard.assets.web.struts.forms.AssetBuscarForm;
import com.mastercard.assets.web.struts.forms.LoginForm;
import com.mastercard.assets.web.util.to.PreimplementacionTO;
import com.mastercard.assets.web.util.to.UserLogin;

public class PreimplementacionEventoAltaIn
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		List< TblFlujo > preimplementationsAct = new ArrayList< TblFlujo >( ) ;
		
//		List< TblFlujo > temp = MasterCardDAO.getAllActivePreImplementationFlows( ) ;
                List< TblFlujo > temp = MasterCardDAO.getAllActiveImplementationFlows( ) ;
                List< TblPreimplementacion > preimpleAct = MasterCardDAO.getAllActivePreimplementationsAndClosed( ) ;
		
		// revisa que los flujos tengan tareas
		for( TblFlujo f : temp )
		{
			// trae las tareas
			List< TblTarea > tareas = MasterCardDAO.getAllActiveTareasWithFlow( f.getCveFlujo( ) ) ;
			
			// en caso de que no sean 0
			if( tareas != null && tareas.size( ) > 0 )
				preimplementationsAct.add( f ) ;
		}
		
		request.setAttribute( "Preimplementations" , preimplementationsAct ) ;
		
                int pais = 0;
//		List< TblAsset > assets = MasterCardDAO.getAllActiveAssets( ) ;
                if( user.getActualRoleId() == 6 )
                {
                    pais = 17 ;
                }else {
                    pais = user.getTblUsuario( ).getTblPais( ).getCvePais( ) ;
                }
                    
                List< TblAsset > assets = MasterCardDAO.getAllActiveAssetsByPais( pais ) ,
                        assetsPais = new ArrayList( ) ;
                
                if( !assets.isEmpty( ) )
                {
                    for( TblAsset a : assets )
                    {
                        boolean yaEsta = false ;
                        if( preimpleAct != null && preimpleAct.size( ) > 0 )
                            for( TblPreimplementacion pre : preimpleAct )
                            {
                                //VERIFICA QUE SEA FLUJO DE IMPLEMENTACION DEPENDIENDO DEL ROL
                                if( user.getActualRoleId( ) != 6 )
                                {
                                    if( pre.getTblFlujo( ).getCveFlujo( ) != 6 )
                                        continue ;
                                } else {
                                    if( pre.getTblFlujo( ).getCveFlujo( ) != 11 )
                                        continue ;
                                }
                                
                                // toma los assets
				List< TblAsset > assetsPre = pre.getTblAssets( ) ;
                                if( assetsPre != null && assetsPre.size( ) > 0 )
                                    for( TblAsset asset : assetsPre )
                                    {
                                        // en caso de ser status == 0 
					if( asset.getStatus( ) == 0 )
                                            continue ;
                                        
                                        // para mostrar depende del rol
                                        if( user.getActualRoleId( ) == 6 )
                                        {
                                            // crea y agrega
                                            if( asset.getTblPais( ).getCvePais( ) == 17 )
                                            {
                                                if( asset == a )
                                                {
                                                    yaEsta = true ;
                                                    break ;
                                                }
                                            }else {
                                                yaEsta = true ;
                                                break ;
                                            }
                                                
                                        } else {
                                            
                                            // crea y agrega
                                            if( asset.getTblPais( ).getCvePais( ) != 17 )
                                            {
                                                if( asset == a )
                                                {
                                                    yaEsta = true ;
                                                    break ;
                                                }
                                            }else {
                                                yaEsta = true ;
                                                break ;
                                            }
                                        }
                                            
                                    }
                            }
                        
                        // para mostrar depende del rol
                                        if( user.getActualRoleId( ) == 6 )
                                        {
                                            // crea y agrega
                                            if( a.getTblPais( ).getCvePais( ) != 17 )
                                            {
                                                
                                                    yaEsta = true ;
                                                    
                                                
                                            }
                                                
                                        } else {
                                            
                                            // crea y agrega
                                            if( a.getTblPais( ).getCvePais( ) == 17 )
                                            {
                                                
                                                    yaEsta = true ;
                                                    
                                                
                                            }
                                        }
                        
                        if( yaEsta == true )
                            continue ;
                        if( pais != 17 )
                        {
                            if( a.getNumDiasViaje( ) == 0 && !a.getTblPaquetes( ).isEmpty( ) )
                             assetsPais.add( a ) ; 
                        }else {
                            if( !a.getTblPaqueteRegional().isEmpty( ) )
                             assetsPais.add( a ) ; 
                        }
                        
                    }
                }
		
		request.setAttribute( "Assets" , assetsPais ) ;
                request.setAttribute( "rol" , "" + user.getActualRoleId( ) );
		
                MasterCardDAO.closeEntityManager( ) ;
	  	return mapping.findForward( "success" ) ;
	}
	
}
